package com.zcr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcr.pojo.Store;
import com.zcr.service.StoreService;
import com.zcr.vo.Result;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RequestMapping("/store")
@RestController
public class StoreController {

    @Resource
    private StoreService storeService;

    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("store/store");
    }

    @Scheduled(initialDelay = 1000,fixedDelay = 5000)
    @GetMapping("/dateStore")
    public ModelAndView dateStore(){
        return new ModelAndView("store/dateStore");
    }

    @GetMapping("/searchAll")
    public Result searchAll(String drugname,Integer page,Integer limit){
        Page<Store> storePage=new Page<>(page,limit);
        IPage<Store> storeIPage=null;

        try {
            storeIPage=storeService.searchAll(drugname,storePage);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        Result result=Result.sucess(storeIPage.getRecords());
        result.setCount(Integer.parseInt(String.valueOf(storeIPage.getTotal())));

    return result;
    }

    @PostMapping("/updateStore")
    public Result updateStore(Store store){
        try {
            storeService.updateStore(store);
            return Result.success("修改药品成功");
        }catch (Exception ex){
            return Result.fail("修改药品失败",ex);
        }
    }

    @PostMapping("/deleteStore")
    public Result deleteStore(Store storeid){
    try {
        storeService.deleteStore(storeid);
        return Result.success("删除药品成功");
    }catch (Exception ex){
        return Result.fail("删除药品失败",ex);
    }
    }

    @PostMapping("/deleteAllStore")
    public Result deleteAllStore(@RequestBody String[] ids){
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
        }
        try {
            storeService.deleteAllStore(ids);
            return Result.success("删除药品成功");
        }catch (Exception ex){
            return Result.fail("删除药品失败",ex);
        }
    }
}
